<!DOCTYPE html>
<html lang="en-us">
  <head>
    <meta charset="UTF-8">
    <base href="https://fastly.jsdelivr.net/gh/fengdh/mdict-js/">
    <title>mdict</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" type="text/css" href="selectize.default.css" media="screen">
    <style>
      #btnLookup {
        border: none;
        height: 36px;
        font-size: 12pt;
        font-weight: bold;
        vertical-align: top;
        border-radius: 3px;
      }

      #btnLookup:not([disabled]) {
        background: #1A4FDD;
        color: white;
      }

      #dict-title {
        display:none;
      }
      
      #word + .selectize-control {
        display: inline-block;
        min-width: 18em;
      }

    </style>
    <script type="text/javascript" src="conf.js"></script>
    <script src="require.js" data-main="mdict"></script>
  </head>
  <body>
        Choose a dictionary file (*.mdx + optional *.mdd): <input id="dictfile" type="file" multiple>
        <p>
        <input id="word" type="text" value="">
        <input id="btnLookup" type="button" value="look up" disabled="false">

        <div id="dict-title"></div>
        <div id="definition">
        </div>
    <script>
      //?file=.../xx.xx,.../xx.xxx#word=
      var dictinput = document.getElementById('dictfile');
      var wordinput = document.getElementById('word');
      var btnLookup = document.getElementById('btnLookup');

      window.onhashchange = function() {
        let v = location.hash.substring(6);
        if(v.length>0){
          wordinput.value = v;
          //wordinput.dispatchEvent(new Event('change', {'bubbles': true}));
          btnLookup.click();
        }
      };

      {
        let v = decodeURIComponent(location.hash.substring(6));
        wordinput.value = v;
      }
      
      {
        let filenames = location.search.substring(6);
        window.onload = function(){
        if(filenames.length>0){
          let clickurl = "i:5fdictinput.click():"+filenames;
          location.href=clickurl;
          //setTimeout(()=>{dictinput.dispatchEvent(new Event('change', {'bubbles': true}));},100);
    
          var callbackTimer = setInterval(function() {
            let files = dictinput.files;
            if(files.length>0){
              if(!btnLookup.disabled){
                clearInterval(callbackTimer);
                return;
              }
            }else
              location.href=clickurl;
            dictinput.dispatchEvent(new Event('change', {'bubbles': true}));
          }, 100);
        
        }
        };
      }
    </script>
  </body>
</html>
